﻿#region Header
// ------------------------ Licence / Copyright ------------------------
// 
// ScrumTable for Scrum Meeting Support
// Copyright © HSR - Hochschule für Technik Rapperswil
// All Rights Reserved
// 
// Author:
//  Michael Gfeller Silvan Gehrig Patrick Boos
// 
// ---------------------------------------------------------------------
#endregion

namespace ScrumTable.Common.Threading
{
    #region Usings

    using System;
    using System.Threading;

    #endregion

    /// <summary>
    /// Contains the global utilities for the ReaderWriterLock class.
    /// Adds a AcquireReaderLock / AcquireWriterLock method in order to
    /// use this method with the using() C# statement.
    /// </summary>
    public static class ReaderWriterLockUtil
    {
        #region Methods
        //--------------------------------------------------------------------
        // Methods
        //--------------------------------------------------------------------

        /// <summary>
        /// Acquires a reader lock with an infinite time-out.
        /// </summary>
        public static IDisposable AcquireReaderLock(this ReaderWriterLock left)
        {
            return new ReaderLockDisposable(left);
        }

        /// <summary>
        /// Acquires a writer lock with an infinite time-out.
        /// </summary>
        public static IDisposable AcquireWriterLock(this ReaderWriterLock left)
        {
            return new WriterLockDisposable(left);
        }

        #endregion

        private class ReaderLockDisposable : DisposeBase
        {
            #region Declarations
            //--------------------------------------------------------------------
            // Declarations
            //--------------------------------------------------------------------

            private readonly ReaderWriterLock _parent;
            
            #endregion

            #region Constructors / Destructor
            //--------------------------------------------------------------------
            // Constructors / Destructor
            //--------------------------------------------------------------------

            internal ReaderLockDisposable(ReaderWriterLock parent)
            {
                PreCondition.AssertNotNull(parent, "parent");

                _parent = parent;
                _parent.AcquireReaderLock(Timeout.Infinite);
            }

            #endregion

            #region Methods
            //--------------------------------------------------------------------
            // Methods
            //--------------------------------------------------------------------

            /// <summary>
            /// Releases managed (c# or .net) resources.
            /// </summary>
            protected override void ReleaseManagedResources()
            {
                _parent.ReleaseReaderLock();
            }

            #endregion
        }

        private class WriterLockDisposable : DisposeBase
        {
            #region Declarations
            //--------------------------------------------------------------------
            // Declarations
            //--------------------------------------------------------------------

            private readonly ReaderWriterLock _parent;

            #endregion

            #region Constructors / Destructor
            //--------------------------------------------------------------------
            // Constructors / Destructor
            //--------------------------------------------------------------------

            internal WriterLockDisposable(ReaderWriterLock parent)
            {
                PreCondition.AssertNotNull(parent, "parent");

                _parent = parent;
                _parent.AcquireWriterLock(Timeout.Infinite);
            }

            #endregion

            #region Methods
            //--------------------------------------------------------------------
            // Methods
            //--------------------------------------------------------------------

            /// <summary>
            /// Releases managed (c# or .net) resources.
            /// </summary>
            protected override void ReleaseManagedResources()
            {
                _parent.ReleaseWriterLock();
            }

            #endregion
        }
    }
}
